package com.hdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdu.dto.CollectCourseDTO;
import com.hdu.entity.CollectCourse;
import com.hdu.mapper.*;
import com.hdu.pojo.PageBean;
import com.hdu.service.ICollectCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdu.vo.AddCollectVo;
import com.hdu.vo.BaseParamVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author xiaobai
 * @since 2025-04-10
 */
@Service
public class CollectCourseServiceImpl extends ServiceImpl<CollectCourseMapper, CollectCourse> implements ICollectCourseService {

    @Resource
    CollectCourseMapper collectCourseMapper;

    @Override
    public PageBean<CollectCourseDTO> queryCollectCourseList(int userInfoId, BaseParamVo baseParamVo) {
        // 分页对象
        Page<CollectCourse> page = new Page<>(baseParamVo.getCurrent(), baseParamVo.getSize());
        // 查询数据内容获得结果
        Page<CollectCourseDTO> pageInfo = collectCourseMapper.querySelectPage(page, userInfoId);
        // 获取数据列表
        List<CollectCourseDTO> items = pageInfo.getRecords();
        // 获取数据总条数
        long counts = pageInfo.getTotal();
        // 构建结果集
        return new PageBean<>(counts, items);
    }

    @Override
    public Boolean createCollectCourse(AddCollectVo addCollectVo) {
        LambdaQueryWrapper<CollectCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectCourse::getUserId, addCollectVo.getUserId());
        wrapper.eq(CollectCourse::getCoursePublishId, addCollectVo.getCoursePublishId());
        if (collectCourseMapper.exists(wrapper)) {
            return true;
        } else {
            CollectCourse collectCourse = new CollectCourse();
            collectCourse.setLearnStatus("205001");
            BeanUtils.copyProperties(addCollectVo, collectCourse);
            System.out.printf(collectCourse.toString());
            collectCourseMapper.insert(collectCourse);
            return false;
        }
    }

    @Override
    public void removeCollectCourse(int userInfoId, Long courseId) {
        LambdaQueryWrapper<CollectCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectCourse::getUserId, userInfoId);
        wrapper.eq(CollectCourse::getCoursePublishId, courseId);
        collectCourseMapper.delete(wrapper);
    }
}
